 /**
 * Description: 物资来源
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.biz.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.biz.mapper.IBizMaterialsSourceMapper;
import com.metro.paperless.biz.service.inf.IBizMaterialsSourceService;
import com.metro.paperless.biz.beans.vo.BizInsideOrg;
import com.metro.paperless.biz.beans.vo.BizMaterialsSource;

 /**
 * 物资来源<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("bizmaterialssourceService")
public class BizMaterialsSourceService extends BaseServiceSupport<BizMaterialsSource> implements IBizMaterialsSourceService{
    //物资来源Mapper
    @Autowired
    private IBizMaterialsSourceMapper bizmaterialssourceMapper;

    @Override
    public IMapper getMapper() {
        return bizmaterialssourceMapper;
    }

    @Override
    public IResult save(BizMaterialsSource entity) {
        try {
            bizmaterialssourceMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资来源失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(BizMaterialsSource entity) {
        try {
            bizmaterialssourceMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资来源失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            bizmaterialssourceMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            bizmaterialssourceMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 判断物资来源是否已存在
     */
    public IResult checkMaterialsSource(BizMaterialsSource entity) {
    	//判断是否已存在
    	List<BizMaterialsSource> list = bizmaterialssourceMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
        		if(list.get(i).getCompanyName().equals(entity.getCompanyName()) && list.get(i).getDataStatus() == 1 ) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    
    /**
     * 修改时判断物资来源是否已存在
     */
    public IResult checkUpdateMaterialsSource(BizMaterialsSource entity) {
    	//修改时判断数据是否修改过
    	List<BizMaterialsSource> list = bizmaterialssourceMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
    			if(list.get(i).getCompanyName().equals(entity.getCompanyName()) && list.get(i).getLinkMan().equals(entity.getLinkMan()) && list.get(i).getLinkMobile().equals(entity.getLinkMobile()) && list.get(i).getRemark().equals(entity.getRemark()) && list.get(i).getDataStatus() == 1) {
    				return ResultCode.DUPLICATE_KEY;
    			}
    		}
    	}
    	return ResultCode.SUCCESS;
    }
    
    
    /**
     * 新增物资
     */
    @Override
    public IResult saveMaterialsSource(BizMaterialsSource entity) {
    	Date date = new Date();
    	entity.setAddTime(date);
    	
        try {
            bizmaterialssourceMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资来源失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 修改物资来源
     */
    @Override
    public IResult updateMaterialsSource(BizMaterialsSource entity) {
        try {
            bizmaterialssourceMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资来源失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    
}